package com.edu.zju.scs.panacea.mapper.resource;

import com.edu.zju.scs.panacea.entity.resource.Loc;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-29 18:30
 */
@Repository
public interface TemplateRelLocMapper {
    @Insert({
            "<script>",
            "insert into res_template_rel_loc(loc_id,template_id) values ",
            "<foreach collection='templateIds' item='item' index='index' separator=','>",
            "(#{locId}, #{item})",
            "</foreach>",
            "</script>"})
    long insertTemplateRelLoc(@Param("locId") Long locId, @Param("templateIds") List<Long> templateIds);


    @Delete("DELETE FROM res_template_rel_loc where loc_id = #{locId}")
    long deleteTemplateRelLoc(Long locId);


    @Select("select template_id FROM res_template_rel_loc where loc_id = #{locId}")
    List<Long> selectTemplatesByLoc(Long locId);

    @Select({
            "<script>",
            "SELECT DISTINCT TRL.loc_id FROM res_template_rel_loc as TRL join res_template as T WHERE T.id = TRL.template_id and T.type = #{type} and TRL.loc_id in (" +
                    "<foreach collection='locIds' item='item' index='index' separator=','>",
            "#{item}",
            "</foreach>" + ")",
            "</script>"})
    Set<Long> selectLocByTemplate(@Param("locIds") Set<Long> locIds, @Param("type") Integer type);
}
